# -*- coding:gbk -*-
import json
import urllib.request
import os

# api key here(https://osu.ppy.sh/p/api)
api_key = ""


def rawmapdata(bid):
    # map data from osu!api
    raw = urllib.request.urlopen(
        "https://osu.ppy.sh/api/get_beatmaps?k=" + api_key + "&b=" + str(bid))
    data = raw.read()
    data = data.decode('gbk')
    data = json.loads(data)
    return data


def rawuserdata(u):
    # user data from osu!api
    raw = urllib.request.urlopen(
        "https://osu.ppy.sh/api/get_user?k=" + api_key + "&type=string&u=" + u)
    data = raw.read()
    data = data.decode('gbk')
    data = json.loads(data)
    return data


def rawuserbpdata(u):
	# user best performance data from osu!api
    raw = urllib.request.urlopen(
        "https://osu.ppy.sh/api/get_user_best?k=" + api_key + "&limit=100&type=string&u=" + u)
    data = raw.read()
    data = data.decode('gbk')
    data = json.loads(data)
    return data

def dlosufile(bid):
	# .osu file from beatmap thread	
    api = rawuserdata(bid)[0]
    urllib.request.urlretrieve("http://osu.ppy.sh/web/maps/" + api["artist"] + "%20-%20" + api[
                               "title"] + "%20(" + api["creator"] + ")%20%5b" + api["version"] + "%5d.osu", r"temp.osu")

def dluserava(u):
	# dl user's ava
    api = rawuserdata(u)[0]
    urllib.request.urlretrieve("http://a.ppy.sh/" + api["user_id"] , r"ava.jpg")
